package cn.edu.scau.cmi.chenyiqiu.comprehension.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateFactory {
	private static  HibernateFactory  hibernateUtil=null;
	private static SessionFactory sessionFactory;
	private static Session session;
	private  HibernateFactory () {};
	public static  HibernateFactory  getInstance() {
		if(hibernateUtil==null) {
			hibernateUtil=new  HibernateFactory();
		}
		return hibernateUtil;
	}
	
	public static SessionFactory getSessionFactory() {
		if(sessionFactory==null) {
			Configuration configuration = new Configuration().configure();
			ServiceRegistry serviceRegistry = configuration.getStandardServiceRegistryBuilder().build();
			sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
		}
		return sessionFactory;
	}
	
	public static Session getSession() {
		if(session==null) {
			session=getSessionFactory().openSession();
		}
		return session;
	}

}
